/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */


package ru.teleology.cubefs.api.reflection;

public final class TypedNumber extends Number {

	public static final int INT 		= 0;
	public static final int LONG 		= 1;
	public static final int BOOLEAN 	= 2;
	public static final int FLOAT 	= 3;
	public static final int DOUBLE 	= 4;
	public static final int SHORT 	= 5;
	public static final int CHAR 		= 6;
	public static final int BYTE		= 7;
		
	public TypedNumber(long handle) {
		super(handle);		
	}

	public TypedNumber(int code, java.lang.Number value) {
		super(newValue(code >= 0 ? code : getValueCode(value)));
		if (value instanceof Integer) {
			setIntValue(value.intValue());
		}
		else if (value instanceof Long) {
			setLongValue(value.longValue());
		}
		else if (value instanceof Float) {
			setFloatValue(value.floatValue());
		}
		else if (value instanceof Double) {
			setDoubleValue(value.doubleValue());
		}
		else if (value instanceof Short) {
			setShortValue(value.shortValue());
		}
		else if (value instanceof Byte) {
			setByteValue(value.byteValue());
		}
		else {
			throw new RuntimeException("Unsupported value type: "+value.getClass().getName());
		}
	}
	
	private static int getValueCode(java.lang.Number value) {
		if (value instanceof Integer) {
			return INT;
		}
		else if (value instanceof Long) {
			return LONG;
		}
		else if (value instanceof Float) {
			return FLOAT;
		}
		else if (value instanceof Double) {
			return DOUBLE;
		}
		else if (value instanceof Short) {
			return SHORT;
		}
		else if (value instanceof Byte) {
			return BYTE;
		}
		else {
			throw new RuntimeException("Unsupported value type: "+value.getClass().getName());
		}
	}
	
	public TypedNumber(char value) {
		super(newValue(CHAR));
		setCharacterValue(value);
	}
	
	public TypedNumber(boolean value) {
		super(newValue(BOOLEAN));
		setBooleanValue(value);
	}
	
	private static native long   newValue(int code);
	
	public boolean equals(Object obj) {
		if (obj instanceof TypedNumber) {
			TypedNumber num = (TypedNumber) obj;
			return this.getLongValue() == num.getLongValue();
		}
		else {
			return false;
		}
	}
}

